package com.roc.web.action.negotiation;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Negotiation;
import com.roc.pojo.Operators;
import com.roc.pojo.TblMchtInf;
import com.roc.service.IExcelService;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

/**
 * 
 * @author Administrator
 * 
 *         代付结果查询
 *
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/negotiation")
public class NegotiationAction extends PaginationBaseAction {

	private Logger logger = LogManager.getLogger(NegotiationAction.class);

	
	@Autowired
	IExcelService excelService;

	@Autowired
	IMerchantServices merchantServices;

	@Autowired
	IPageService pagenitionService;
	
	@Autowired
	private IPowerInfoService powerInfoService;
	
	private NegotiationSearchCondition searchCondition;

	protected Map<String, String> tranMap = ConfigProperties.getTransTypes();;

	protected Map<String, String> respMap = ConfigProperties.getRespCodeTypes();
	protected Map<String, String> allRespMap = ConfigProperties
			.getRespCodeQueryTypes();
	protected Map<String, String> reversalMap = ConfigProperties
			.getReversalTypes();

	@Action(value = "queryNegotiation", results = { @Result(name = "success", location = "/pages/negotiation/queryNegotiation.jsp") })
	public String queryNegotiAtion() throws Exception {

		if (searchCondition == null) {
			searchCondition = new NegotiationSearchCondition();
		}

		searchCondition.setTranDate(Constance.formatDate("yyyyMMdd"));
		getSession().setAttribute("tranMap", tranMap);
		getSession().setAttribute("respMap", respMap);
		getSession().setAttribute("allRespMap", allRespMap);
		return SUCCESS;
	}

	// 代付结果查询
	@Action(value = "queryNegotiationForm", results = { @Result(name = "success", location = "/pages/negotiation/queryNegotiation.jsp") })
	public String negotiationAction() throws Exception {
		Operators logingMerchant = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		
		
		  
		try {
			if (searchCondition == null) {
				searchCondition = (NegotiationSearchCondition) getSearchConditionFromSession();
				if (searchCondition == null) {
					searchCondition = new NegotiationSearchCondition();
				}
			}
			 if(searchCondition.getMinDate()==null||searchCondition.getMinDate().equals("")){
				 logger.info("代付流水查询时间为空");
				   return SUCCESS;
			   }
			 
			 Map<String, String> map=powerInfoService.queryRealIdByOpId(logingMerchant.getSuperiorId(), logingMerchant.getRoleType(), logingMerchant.getId(), logingMerchant.getOrgCode());
	           // String orgCode=map.get("orgCode");
	         String superRole= map.get("superRole");
	         String opRealId =map.get("opRealId");
			// 当登录人员为代理商时，查询他的子代理
			searchCondition.setRoleType(superRole);
			if(Role.MERCHANT.getCode().equals(superRole)){
				searchCondition.setMerNo(logingMerchant.getAccountNo());
			}else if(Role.AGENT.getCode().equals(superRole)||Role.SFB_AGENT.getCode().equals(superRole)){
				
				String agentId= opRealId;
				searchCondition.setObj(agentId);
				
			}else if(Role.AGENT_BUSS.getCode().equals(superRole)){
				searchCondition.setOpid(opRealId);
			}else if(Role.MARKET_DIRECT.getCode().equals(superRole) || Role.MARKET_LEAD.getCode().equals(superRole)){
				searchCondition.setMarketorId(opRealId);
			}else if(Role.O_MANAGER.getCode().equals(superRole)){//
				searchCondition.setUcbccdArea(logingMerchant.getOneagentid());
			}/*else if(Role.OPERATORS_COMM.getCode().equals(logingMerchant.getRoleType()) && !Constant.SD_ORG_CD.equals(logingMerchant.getOneagentid())){//运维人员
				searchCondition.setUcbccdArea(logingMerchant.getOneagentid());
			}*/else{
				searchCondition.setOrgCode(logingMerchant.getOrgCode());
			}
			int rows = pagenitionService.countSQL(searchCondition);
			if (rows > 0) {
				searchRsList = pagenitionService.findAllSQL(searchCondition,
						calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
			List<Negotiation> negotiationList = new ArrayList<Negotiation>();
			
			if (searchRsList != null && searchRsList.size() > 0) {
				Iterator iterator = searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj = (Object[]) iterator.next();
					Negotiation nego = new Negotiation();
					nego.setTransDt(Constance.Null2EmptyTrim(obj[0]));
					nego.setTransTm(Constance.Null2EmptyTrim(obj[1]));
					nego.setRespCd(Constance.Null2EmptyTrim(obj[2]));
					nego.setTranSeq(Constance.Null2EmptyTrim(obj[3]));
					nego.setMchntCd(Constance.Null2EmptyTrim(obj[4]));
					nego.setMchntNm(Constance.Null2EmptyTrim(obj[5]));
					nego.setPyeBankNm(Constance.Null2EmptyTrim(obj[6]));
					nego.setPyeAcctNo(Constance.Null2EmptyTrim(obj[7]));
					nego.setPyeAcctNm(Constance.Null2EmptyTrim(obj[8]));
					nego.setTranAmt(Constance.Null2EmptyTrim(obj[11]));
					negotiationList.add(nego);
				}
			}
			getRequest().setAttribute("negotiationList", negotiationList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			addActionMessage("当前条件下统计到【" + rows + "】条记录");
		} catch (ServiceException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			addActionError(e.getMessage());

		}

		return SUCCESS;

	}

	public NegotiationSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(NegotiationSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_BEAN_ESTRN_LOG_YJ,
				searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (NegotiationSearchCondition) getSession().getAttribute(
				SEARCH_SESSION_BEAN_ESTRN_LOG_YJ);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE + "";
	}

}
